/**
 * 切换城市JS处理
 */

//获取到城市选择
var city_belongs = $("head meta[http-equiv='city_belong_default']").attr('content');
city_belongs='';//不定位城市
if (city_belongs == 'default') {
    //通过获取IP定位城市
    $.ajax({
        type: "get",
        url: "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",
        cache: true,
        async: false,
        dataType: "script",
        success: function () {
            var get_city = remote_ip_info.city;
            jQuery.getScript('/user/action_save_city?action=name&format=js&city_name=' + get_city, function () {
                if (city) {
                    $('font.citys').html(city);
                    var exp = new Date();
                    exp.setTime(exp.getTime() + 365 * 30 * 24 * 60 * 60 * 1000);
                    document.cookie = "now_city_name=" + city + ";domain=.8now.com;expires=" + exp.toGMTString();

                    location.reload();
                } else {
                    //alert('无法定位您的城市，请手工选择！');
                }
            });
        }

    });
    /*
     jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function () {
     var get_city = remote_ip_info.city;
     jQuery.getScript(base_url_www + 'user/action_save_city?action=name&format=js&city_name=' + get_city, function () {
     if (city) {
     $('font.citys').html(city);
     location.reload();
     } else {
     alert('无法定位您的城市，请手工选择！');
     }
     });
     });*/
}
$(document).ready(function () {
//切换城市的弹窗渲染
    $(".replace-city").on('click', function () {
        console.log(21);
        if ($("#notice-panel-city #div-city").html() == '') {
            jQuery.getScript('/search/ajaxGetcity', function () {
                var str = _show_change_city_tab(data);
                if (str) {
                    $("#notice-panel-city #div-city").html(str);
                    $('#notice-panel-city').foundation('reveal', 'open');
                }
            });
        } else {
            $('#notice-panel-city').foundation('reveal', 'open');
        }
    });
    //选择省份
    $(document).on("click", '.choosecity', function () {
        $('.choosecity').css('background', '#2f7f8c');
        $(this).css('background', '#F90');
        var val = $(this).attr('value');
        $('.zone_all').hide();
        $('#zone_one_' + val).show();
    });
    //选择切换的城市
    $(document).on("click", '.choosecolor', function () {
        var cityid = $(this).attr('value');
        jQuery.getScript('/user/action_save_city?action=id&format=js&city_id=' + cityid, function () {
            if (city) {
                $('font.citys').html(city);
                location.href="http://www.8now.com/roommate/user_search";
                // location.reload();
            } else {
                alert('城市切换失败，请重试！');
            }
        });
    });
    //切换城市的渲染HTML
    var _show_change_city_tab = function (data) {
        if (!data.province)
            return false;
        var str = '';
        /*$.each(data, function (provinceid, city) {*/
            str += '<div class="row.maxth">';
            /*if (provinceid == 'hot') {*/
                str += '<table width="100%">';
                str += '<tr>';
                str += '<td><h4 class="section-title" style="margin-bottom: -20px;">热门城市：</h4></td>';
                str += '</tr>';
                str += '<tr>';
                str += '<td class="col">';
                str += '<div class="large-1 small-4 medium-2 columns label-set end">';
                str += '<span class="label from-select button radius choosecolor" t="0" value="45386">不限</span>';
                str += '</div>';
                str += '<div class="large-1 small-4 medium-2 columns label-set end">';
                str += '<span class="label from-select button radius choosecolor" t="0" value="292">珠海</span>';
                str += '</div>';
              /*  $.each(city, function (hotid, hotname) {*/
                    str += '<div class="large-1 small-4 medium-2 columns label-set end">';
                   /* str += '<span class="label from-select button radius choosecolor" t="0" value="' + hotid + '">' + hotname + '</span>';*/
                    str += '<span class="label from-select button radius choosecolor" t="0" value="306">中山</span>';
                    str += '</div>';
                /*});*/
                str += '</td>';
                str += '</tr>';
                str += '</table>';
            /*}*/
           /* if (provinceid == 'province') {
                str += '<table width="100%" style="margin-top: -10px;">';
                str += '<tr>';
                str += '<td><h4 class="section-title" style="margin-bottom: -20px;">省份：</h4></td>';
                str += '</tr>';
                str += '<tr>';
                str += '<td>';
                $.each(city, function (hotid, hotname) {
                    str += '<div class="large-1 small-4 medium-2 columns label-set end">';
                    var s = '';
                    if (hotid == 3)
                        s = '#F90';
                    str += '<span class="label success button radius choosecity" value="' + hotid + '" style="background:' + s + '">' + hotname + '</span>';
                    str += '</div>';
                });
                str += '</td>';
                str += '</tr>';
                str += '</table>';
            }
            if (provinceid == 'city') {
                str += '<table width="100%" style="margin-top: -22px;">';
                str += '<tr>';
                str += '<td><h4 class="section-title" style="margin-bottom: -20px; margin-top: -10px;">城市：</h4></td>';
                str += '</tr>';
                $.each(city, function (hotid, hotname) {
                    var style = '';
                    if (hotid == 3)
                        style = 'display: table-row;';
                    else
                        style = 'display: none;';
                    str += '<tr id="zone_one_' + hotid + '" class="zone_all" style="' + style + '">';
                    str += '<td class="col">';
                    $.each(hotname, function (zoneid, zonename) {
                        str += '<div class="large-1 small-4 medium-2 columns label-set end">';
                        str += '<span class="label from-select button radius choosecolor" t="0" value="' + zoneid + '">' + zonename + '</span>';
                        str += '</div>';
                    });
                });
                str += '</tr>';
                str += '</table>';
            }*/
            str += '</div>';
        /*});*/
        return str;
    }
});